package com.apache.funds.service.plugins;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.funds.manager.SvmCaptionBalanceManager;
import com.apache.funds.util.Constants;
import com.apache.funds.util.FundsTemplate;
import com.apache.funds.vo.UserFundsVo;
import com.apache.tools.DateUtils;
import com.apache.tools.DateUtils.FmtStr;

public class FundsEnoughPlugin implements PluginConnector {

	private SvmCaptionBalanceManager svmCaptionBalanceManager;

	public void setSvmCaptionBalanceManager(SvmCaptionBalanceManager svmCaptionBalanceManager) {
		this.svmCaptionBalanceManager = svmCaptionBalanceManager;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tradeDate", DateUtils.getDate());
		long time = DateUtils.getDate().getTime();
		String dateTime = DateUtils.longToStr(time, FmtStr.yyyyMMdd);
		long dateTrade = DateUtils.toLong(DateUtils.parse(dateTime));
		vo.setParams("dateTrade", dateTrade);
		Date d = DateUtils.fromLong(dateTrade);
		Date yd = DateUtils.beforeNDays(d, 1);
		long dateTradeYesterday = DateUtils.toLong(yd);
		vo.setParams("dateTradeYesterday", dateTradeYesterday);
		List list = (List) svmCaptionBalanceManager.execute(vo);
		ResultEntity re = new ResultEntity();
		String tradeType = String.valueOf(vo.getParams("tradeType"));
		if (list != null && !list.isEmpty()) {
			UserFundsVo uf = (UserFundsVo) list.get(0);
			String operAmt = String.valueOf(vo.getParams("amount"));
			long amount = Long.valueOf(operAmt);
			re.setResult("true");
			re.setMessage("");
			re.setEntity("true");

			//可能额度
			long m1 = Long.valueOf(0);
			//解冻客户
			long m2 = Long.valueOf(0);

			if (tradeType != null && !"".equals(tradeType) && !"null".equals(tradeType)) {
				List<FundsTemplate> templates = Constants.getStorageTemplate(tradeType).get(tradeType);
				// 利用反射机制，判断每一个交易码里所有为out的金额是否小于当前amount的数量
				Field[] fields = uf.getClass().getDeclaredFields();
				for (FundsTemplate st : templates) {
					if (st.getDirection().equals("out")) {
						for (Field field : fields) {
							if (st.getCaption().equals(field.getName())) {
								field.setAccessible(true);
								m1 = Long.valueOf(String.valueOf(field.get(uf)));
								if (m1 < amount) {
									re.setResult("true");
									re.setMessage(Constants.Msg.F008+"资金不足");
									re.setEntity("false");
									return re;
								}
							}
						}
					}
				}
			}

		} else {
			re.setResult("true");
			re.setEntity("false");
			re.setMessage(Constants.Msg.F008+"您的余额不足");
		}
		//		UserFunds uf = (UserFunds) list.get(0);
		//		String operAmt = String.valueOf(vo.getParams("amount"));
		//		long amount = Long.valueOf(operAmt);
		//		long userFund = uf.getM1();
		//		ResultEntity re = new ResultEntity();
		//		re.setResult("true");
		//		re.setMessage("");
		//		re.setEntity("true");
		//		if ((userFund - amount) < 0l) {
		//			re.setEntity("false");
		//			re.setMessage("您的余额不足");
		//		}
		return re;
	}

}
